<?php

namespace common\behavior\CommonMessagePushing;


use common\model\UserModel;
use common\service\BehaviorService;
use common\service\OptionsService;
use common\service\TaskAssembleService;
use Webman\Stomp\Client;

/**
 * 任务发布 消息通知
 * 通知下游任务执行人：文件有更新
 * 工序任务下游、标准任务下游
 */
class TaskFileInformingBehavior
{
    /**
     * @param $params
     * @return void
     */
    public function run($params)
    {
        /***********  预处理开始  ************/
        $params = BehaviorService::getInstance()->dealParam(
            $params,
            [
                'task' => '',
                'entity' => '',
                'current_task_name' => 'strval',
                'partner' => 'array_map,intval',
            ],
            [
                'task' => [],
                'entity' => [],
                'current_task_name' => '',
                'partner' => [],
            ],
            'data',
            ['type' => 'task_finish_informing_downstream'],
        );
        /***********  行为判断  ************/
        if ($params === false) {
            return;
        }
        /***********  变量迁移  ************/
        list($task, $entity, $currentTaskName, $partner) = $params;
        if (empty($task) || empty($currentTaskName)) {
            return;
        }

        /***********  预处理结束  ************/

        /***********  处理消息逻辑  ************/
        $notifyUserIdList = [];
        if (in_array($task['step_type'], ['step'])) {
            if ($task['assignee_producer'] > 0) {
                $notifyUserIdList[$task['assignee_producer']] = $task['assignee_producer'];
            }
            if ($task['executor'] > 0) {
                $notifyUserIdList[$task['executor']] = $task['executor'];
            }
            $taskName = TaskAssembleService::getInstance()->getNotifyTaskName($entity, $task);
        } else {
            foreach ($partner as $userId) {
                $notifyUserIdList[$userId] = $userId;
            }
            $taskName = TaskAssembleService::getInstance()->getNotifyTaskName([], $task);
        }

        $first = '任务';
        $first .= $taskName . '的上游任务' . $currentTaskName . '发布了新的文件，请前往更新。';
        $notifyParam = [
            'first' => '上游有更新',
            'note_text' => $first,
            'remark' => "请及时更新",
            'tenant_id' => $task['tenant_id'],
            'project_id' => $task['project_id'],
            'task_id' => $task['id'],
            'module_code' => in_array(module_by_id($task['entity_module_id'])['code'], ['design', 'level', 'asset']) ? 'asset' : 'shot',
            'message_key' => 'task_upstream_file_update',
        ];
        $saasUserList = [];
        if (!empty($notifyUserIdList)) {
            $tempSaasUserList = model(UserModel::class)->where(['id' => ['IN', implode(',', $notifyUserIdList)]])->select();
            $saasUserList = array_merge($saasUserList, array_column($tempSaasUserList, null, 'id'));
        }
        if (empty($saasUserList)) {
            return;
        }
        $templateId = OptionsService::getInstance()->getTaskRemindCommonTemplateId();

        $messageGenerated = [];
        foreach ($saasUserList as $saasUserItem) {
            $messageGenerated[] = [
                "param" => $notifyParam,
                'receiver' => $saasUserItem['union_id'],
                'template_id' => $templateId,
                'content_type' => 'card_message',
                'notice_type' => 'message_file',
                'is_popup' => 1,
                'message_key' => $notifyParam['message_key'],
                'project_id' => $task['project_id'],
            ];

        }
        /***********  发送新的队列  ************/
        foreach ($messageGenerated as $message) {
            Client::send('common_message_pushing', build_queue_data_with_xu([
                'message_generated' => [$message],
            ]));
        }
        /***********  结束  ************/
    }
}
